#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from confignb import ConfigNB
from informationnb import InformationNB
import urllib2
import urllib
import os
from xml.dom import minidom
from actionsnb import ActionsNB
import subprocess
import base64
import time

class ConectionNB:

	def __init__(self):
		self.__config = ConfigNB()
		self.__url = self.__config.getWebServiceConfig()["url"]
		self.__passwdDevice = self.__config.getWebServiceConfig()["passwordDevice"]
	
	def sendData(self):
		information = InformationNB()
		actions = ActionsNB()
		
		mac = information.getMac()
		so = information.getSistemaOperativo()
		arch_cpu = information.getArquitecturaCpu()
		ip_externa = information.getIpExterna()
		ip_interna = information.getIpInterna()
		listado_proc = information.getListadoProcesos()
		
		actions.tomarScreenshot()
		path_screenshot = "screenShot.jpeg"
		
		actions.tomarFotoWebcam()
		path_fotowebcam = "webcamShot.jpeg"
		
		geo_info = information.getGeolocation()
		geo_x = geo_info['latitude']
		geo_y = geo_info['longitude']
		info_red = information.getInfoRed()
		key_report = self.__passwdDevice
		try:
			screenshot_content = open(path_screenshot,'rb').read()
		except:
			print "Problemas al abrir el screenshot"
			screenshot_content = "No data"
		try:
			fotowebcam_content = open(path_fotowebcam,'rb').read()
		except:
			print "Problemas al abrir la foto de webcam"
			fotowebcam_content = "No data"
		
		try:
			os.remove('screenShot.jpeg')
		except:
			print "Problemas al eliminar screenshot"
		
		try:
			os.remove('webcamShot.jpeg')
		except:
			print "Problemas al eliminar camshoot"
		
		sendData = {
				'mac': mac,
				'path_screenshot': base64.b64encode(screenshot_content),
				'path_fotowebcam': base64.b64encode(fotowebcam_content),
				'so': so,
				'arch_cpu': arch_cpu,
				'ip_externa': ip_externa,
				'ip_interna': ip_interna,
				'listado_proc': listado_proc,
				'geo_x': geo_x,
				'geo_y': geo_y,
				'info_red': info_red,
				'key_report': base64.b64encode(key_report)
				}
		
		url = self.__url+"/xml/reportes/create.php"
		
		try:
			req = urllib2.Request(url=url, data=urllib.urlencode(sendData))
			response = urllib2.urlopen(req, timeout = 90)
			print "Datos enviados correctamente..."
			print response.read()
		except:
			print "Problemas al enviar el reporte"
	
	def requestData(self):
		try:
			urlRequestData = self.__url+"/xml/getconfig/getconfig.php?key_config="+self.__passwdDevice
			data = urllib2.urlopen(urlRequestData, timeout = 90).read()
				
			doc = minidom.parseString(data)
			elements = doc.getElementsByTagName("configuracion")
			
			self.__config.setDevicePerdido(elements[0].getAttribute("perdido"))
			self.__config.setTimeSendData(elements[0].getAttribute("intervalos_reportes"))
			self.__config.setAlertActive(elements[0].getAttribute("alerta"))
			self.__config.setMsgAlert(elements[0].getAttribute("msg_alerta"))
			self.__config.setPcBloqueadoConfig(elements[0].getAttribute("bloquear"))
			self.__config.setKillProcessActive(elements[0].getAttribute("matar_proceso"))
			self.__config.setProcessNameKill(elements[0].getAttribute("proceso_matar"))
			self.__config.setFtpRunStartup(elements[0].getAttribute("ftp"))
			self.__config.setFtpServerConfig(elements[0].getAttribute("ftp_user"),elements[0].getAttribute("ftp_password"),elements[0].getAttribute("ftp_path"))
		except:
			print "Imposible actualizar la configuración"
			